<template>
  <el-dialog title="零公里工单选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal=false
             width="80%"
             center
  >
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编号" prop="workOrderCode">
        <el-input
          v-model="queryParams.workOrderCode"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="clientCode">
        <el-input
          v-model="queryParams.clientCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-card style="border-radius: 10px">
    <el-table v-loading="loading" :data="zeroList" border @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedRepairId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="工单编号" align="center" prop="workOrderCode" />
      <el-table-column label="工单类型" align="center" prop="workOrderType" />
      <el-table-column label="客户名称" align="center" prop="clientName" />
<!--      <el-table-column label="工单状态" align="center" prop="workOrderStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.workOrderStatus === '草稿'" type="info">草稿</el-tag>
          <el-tag v-if="scope.row.workOrderStatus === '已提交'" type="warning">已提交</el-tag>
          <el-tag v-if="scope.row.workOrderStatus === '已处置'" type="danger">已处置</el-tag>
          <el-tag v-if="scope.row.workOrderStatus === '已整改'">已整改</el-tag>
          <el-tag v-if="scope.row.workOrderStatus === '已完成'" type="success">已完成</el-tag>
        </template>
      </el-table-column>-->
      <el-table-column label="联系人" align="center" prop="person" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['as:zero:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['as:zero:edit']"
            v-if="scope.row.workOrderStatus === '草稿'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['as:zero:remove']"
            v-if="scope.row.workOrderStatus === '草稿'"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleDisposition(scope.row)"
              v-hasPermi="['as:zero:disposition']"
              v-if="scope.row.workOrderStatus === '已提交'"
          >异议处置</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleRectify(scope.row)"
              v-hasPermi="['as:zero:rectify']"
              v-if="scope.row.workOrderStatus === '已处置'"
          >整改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleCheck(scope.row)"
              v-hasPermi="['as:zero:check']"
              v-if="scope.row.workOrderStatus === '已整改'"
          >验证</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listZero, getZero, delZero, addZero, updateZero } from "@/api/mes/as/zero";
export default {
  name: "Zero",

  data() {
    return {
      selectedRows: undefined,
      showFlag:false,
      selectedRepairId: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 零公里工单表格数据
      zeroList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrderCode: null,
        workOrderType: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        clientNick: null,
        clientAddress: null,
        workOrderStatus: null,
        person: null,
        phone: null,
        disposalDeptId: null,
        disposalDept: null,
        url: null,
        disposalPerson: null,
        disposalTime: null,
        disposalPrice: null,
        disposalWay: null,
        disposalAmount: null,
        disposalResult: null,
        rectifyDeptId: null,
        rectifyDept: null,
        rectifyPerson: null,
        rectifyTime: null,
        rectifyResult: null,
        checkPerson: null,
        checkTime: null,
        checkResult: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workOrderCode: [
          { required: true, message: "工单编号不能为空", trigger: "blur" }
        ],
        clientName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],

      },
      //自动生成编码
      autoGenFlag:false,
      // 标识
      optType: undefined,
      // 部门树选项
      deptOptions: undefined,
      // 上传文件类型
      fileType: [
        // 文档类
        "pdf", "doc", "docx", "xls", "xlsx",
        // 图片类
        "jpg", "jpeg", "png", "gif", "bmp",
        // 视频类
        "mp4", "avi", "rmvb", "mkv", "mov",
        // 压缩文件类
        "zip", "rar", "7z", "tar", "gz"
      ],
      // 上传文件列表
      fileList: [],
      // 部门数组
      deptList: [],
      // 处置方式数组
      disposalWayOptions: [
        {label: "整改", value: "rectify"},
        {label: "处罚", value: "punish"},
        {label: "整改+处罚", value: "rectify+punish"},
      ],
      // 当前登录用户
      user: null,
      // 整改部门
      rectifyList: [],
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getUserProfile();
  },
  methods: {
    /** 查询零公里工单列表 */
    getList() {
      this.loading = true;
      this.selectedRows=[]
      listZero(this.queryParams).then(response => {
        this.zeroList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
        this.selectedRows=[]
      }
    },
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRepairId == null || this.selectedRepairId == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
      this.selectedRows=[]
    },
    // 取消按钮
    cancel() {
      this.showFlag = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workOrderCode: null,
        workOrderType: '零公里工单',
        clientId: null,
        clientCode: null,
        clientName: null,
        clientNick: null,
        clientAddress: null,
        workOrderStatus: "草稿",
        person: null,
        phone: null,
        disposalDeptId: null,
        disposalDept: null,
        remark: null,
        url: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        disposalPerson: null,
        disposalTime: null,
        disposalPrice: null,
        disposalWay: null,
        disposalAmount: null,
        disposalResult: null,
        rectifyDeptId: null,
        rectifyDept: null,
        rectifyPerson: null,
        rectifyTime: null,
        rectifyResult: null,
        checkPerson: null,
        checkTime: null,
        checkResult: null
      };
      this.deptList = [];
      this.rectifyList = [];
      this.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加零公里工单";
      this.optType = 'add';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const id = row.id || this.ids
      getZero(id).then(response => {
        this.form = response.data;
        response.data.url === null ? this.fileList = [] : this.fileList = response.data.url.split(',');
        if (response.data.disposalDept) {
          this.deptList = response.data.disposalDept.split(',');
        }
        if (response.data.rectifyDept) {
          this.rectifyList = response.data.rectifyDept.split(',');
        }
        this.open = true;
        this.title = "修改零公里工单";
        this.optType = 'update'
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getZero(id).then(response => {
        this.form = response.data;
        response.data.url === null ? this.fileList = [] : this.fileList = response.data.url.split(',');
        if (response.data.disposalDept) {
          this.deptList = response.data.disposalDept.split(',');
        }
        if (response.data.rectifyDept) {
          this.rectifyList = response.data.rectifyDept.split(',');
        }
        this.open = true;
        this.title = "查看零公里工单";
        this.optType = 'view'
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.disposalDept = this.deptList.join(',');
          this.form.rectifyDept = this.rectifyList.join(',');
          if (this.form.id != null) {
            updateZero(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZero(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除零公里工单编号为"' + ids + '"的数据项？').then(function() {
        return delZero(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('as/zero/export', {
        ...this.queryParams
      }, `zero_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
